namespace hjc {
	export class Platform implements IPlatform {
		protected $authSetting = {};
		protected $token = null;

		//Getters&Setters
		public get authSetting(): any { return this.$authSetting };
		public get token(): string { return this.$token };
		public get isMobile(): boolean { return egret.Capabilities.isMobile; }
		public get language(): string { return egret.Capabilities.language; }
		public get isZhCN(): boolean { return this.language === 'zh-CN'; }
		public get os(): string { return egret.Capabilities.os; };
		public get renderMode(): string { return egret.Capabilities.renderMode; }
		public get runtimeType(): string { return egret.Capabilities.runtimeType; }
		public get isWx(): boolean { return this.runtimeType.toLowerCase() === 'wxgame'; }
		public get isWeb(): boolean { return this.runtimeType.toLowerCase() === 'web'; }
		public get isIOS(): boolean { return this.os.toLowerCase() === 'ios'; }
		public get isAndroid(): boolean { return this.os.toLowerCase() === 'android'; }

		//third platform
		public launch(nexts?: hjc.TVisitNext): void {
			if (this.isWx) {
				wx.getSetting({
					success: res => this.$authSetting = res.authSetting,
					fail: () => tools.popErr('获取微信授权信息失败！'),
					complete: () => {
						if (this.isAuthorized()) wx.checkSession({
							success: () => {
								if (notEmpty(this.$token) && isFunc(nexts.reconnect)) nexts.reconnect();
								else if (isEmpty(this.$token) && isFunc(nexts.login)) nexts.login();
							}, fail: () => { if (notEmpty(nexts) && isFunc(nexts.login)) nexts.login(); }
						}); else if (notEmpty(nexts) && isFunc(nexts.register)) nexts.register();
					}
				});
			} else if (this.isWeb) {
				if (notEmpty(this.$token) && isFunc(nexts.reconnect)) nexts.reconnect();
				else if (isEmpty(this.$token) && isFunc(nexts.login)) nexts.login();
			} else console.warn('undefined platform type...');
		}

		public isAuthorized(type?: string): boolean {
			if (this.isWx) {
				if (notEmpty(type)) return this.$authSetting[`scope.${type}`] === true;
				else return this.$authSetting['scope.userInfo'] === true;
			} else {
				console.warn('platform method \'isAuthorized\' hasn\'t realized...');
				return true;
			}
		}

		public forcedAuthTips(content?: string, title?: string): void {
			if (this.isWx) wx.showModal({
				title: notEmpty(title) ? title : '友情提醒',
				content: notEmpty(content) ? content : '该游戏需获微信授权!',
				confirmText: "确定",
				showCancel: false,
			}); console.warn('platform method \'forcedAuthTips\' hasn\'t realized...');
		}

		public async requestAuth(img?: string, options?: TGameSize): Promise<any> {
			if (this.isWx) {
				if (isEmpty(img) || isEmpty(options)) { console.warn('wx platform requires params: \'img\' & \'options\''); return null; }
				else return new Promise(resolve => {
					tools.showUserInfoBtn(img, options, (res: wx.TAuthResult) => {
						tools.hideUserInfoBtn();
						resolve(res);
					});
				});
			} else console.warn('platform method \'requestAuth\' hasn\'t realized...');
		}

		public async requestUserInfo(): Promise<any> {
			if (this.isWx) return new Promise((resolve, reject) => {
				wx.getUserInfo({
					withCredentials: true,
					success: res => resolve(res.userInfo as wx.UserInfo),
					fail: res => resolve(null),
				})
			});
			else console.warn('platform method \'requestUserInfo\' hasn\'t realized...');
		}

		public async requestLoginInfo(): Promise<any> {
			if (this.isWx) return new Promise(resolve => {
				wx.login({ success: res => resolve(res.code) });
			});
			else console.warn('platform method \'requestLoginInfo\' hasn\'t realized...');
		}
	}
}

